/**
 * Live GPS Tracking Application
 * MIDlet Source
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens.gpstrack;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import nl.ivarclemens.gpsdriver.Bluetooth;

import nl.ivarclemens.gpstrack.ui.*;
import nl.ivarclemens.gpstrack.nmea.*;
import nl.ivarclemens.gpstrack.track.*;

public class GPSTrack extends MIDlet implements CommandListener {
    private Display display;

    private Config config = null;

    private Bluetooth btDriver = null;
    private GPSThread gpsThread = null;
    private HTTPThread httpThread = null;
    private NMEA nmeaParser = null;

    // Screens
    MenuUI menuScreen = null;

    ConfigUI configScreen = null;
    MapUI mapScreen = null;
    StatusUI statusScreen = null;
    TrackUI trackScreen = null;

    public void gotoScreen(String screen)
    {
        if(screen.compareTo("config") == 0)
            display.setCurrent(configScreen);
        else if(screen.compareTo("status") == 0)
            display.setCurrent(statusScreen);
        else if(screen.compareTo("map") == 0)
            display.setCurrent(mapScreen);
        else if(screen.compareTo("track") == 0)
            display.setCurrent(trackScreen);
        else
            display.setCurrent(menuScreen);
    }

    public void startApp() {
        config = new Config();
        display = Display.getDisplay(this);

        // Initialize GPS reader thread
        btDriver = new Bluetooth();
        btDriver.openConnection(config.getOption("DeviceAddress"));

        nmeaParser = new NMEA();

        gpsThread = new GPSThread(btDriver);
        gpsThread.setListener(nmeaParser);

        // Initilize live tracking
        //LiveTrack liveTrack = new LiveTrack();
        //nmeaParser.addPositionListener(liveTrack);

        // Initialize HTTP sender (uses it's own buffer for now)
        httpThread = new HTTPThread();
        httpThread.setDestination(config.getOption("ServerAddress"));
        nmeaParser.addPositionListener(httpThread);

        /////////////////////////////////////////////////////////

        // Initialize screens
        menuScreen = new MenuUI(this);
        statusScreen = new StatusUI(this);
        mapScreen = new MapUI(this);
        configScreen = new ConfigUI(this);
        trackScreen = new TrackUI(this, httpThread);

        mapScreen.getMap().addTrack(httpThread);

        // Show main screen
        this.gotoScreen("menu");    
       
        //TrackList tracks = NetworkSource.loadTracks("http://bata.ivarclemens.nl/data/stages");
        //map.addTrackList(tracks);
    }

    public void toggleNetwork()
    {
        if(httpThread.mayTransmit()) {
            httpThread.stopTransmission();
            mapScreen.stopTransmission();
        } else {
            httpThread.startTransmission();
            mapScreen.startTransmission();
        }
    }

    public void setNetwork(boolean state)
    {
        if(state == true) {
            httpThread.startTransmission();
            mapScreen.startTransmission();
        } else {
            httpThread.stopTransmission();
            mapScreen.stopTransmission();
        }
    }

    public NMEA getNMEAParser()
    {
        return nmeaParser;
    }

    /**
     * Returns configuration object to child UI.
     */
    public Config getConfig()
    {
        if(this.config == null)
            this.config = new Config();

        return this.config;
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if(c.getCommandType() == Command.EXIT)
            notifyDestroyed();
    }
}
